package com.rulin.asp.business.datacleaning.batchimport;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rulin.asp.business.datacleaning.entity.QuestionNew;
import com.rulin.asp.business.datacleaning.mapper.QuestionNewMapper;
import com.rulin.asp.business.datacleaning.util.Response;
import com.rulin.asp.business.entity.Dictionary;
import com.rulin.asp.business.mapper.DictionaryMapper;
import com.rulin.asp.sso.entity.UserUtils;
import com.rulin.asp.util.ExcelUtils;

@Service
public class UploadImpl implements Upload{

	@Autowired
	private QuestionNewMapper questionNewMapper;
	
	@Autowired
	private DictionaryMapper dictionaryMapper;
	
	@Transactional
	@Override
	public Response batchQuestionUpload(InputStream is, String fileName) throws Exception {
		QuestionResponse response = new QuestionResponse();
		List<List<Object>> list = null;
		try {
			list = ExcelUtils.getListByExcel(is, fileName);
		}catch(Exception e) {
			e.printStackTrace();
			response.setCode(QuestionResponse.SERVER_ERROR);
			response.setMessage(e.getCause().getMessage());
			response.setErrorMessage(e.getMessage());
		}
		Map<String, String> majorMaps = getMajorMaps();
		Map<String, String> subjectMaps = getSubjectMaps();
		List<QuestionNew> questions = new ArrayList<QuestionNew>();
		String uploadUid = UserUtils.getUser().getId();
		for (int i = 0; i < list.size(); i++) {
			List<Object> row = list.get(i);
			if(row.size() < 9) {
				response.setCode(QuestionResponse.DATA_NULL_ERROR);
				response.setMessage("第"+(i+2)+"行数据缺失，请确认");
				return response;
			}
			QuestionNew questionNew = new QuestionNew();
			questionNew.setUploadUid(uploadUid);
			String id = UUID.randomUUID().toString();
			Integer quesNo = Integer.valueOf((String) row.get(0));
			String question = (String) row.get(1);//问题
			String major = (String) row.get(2);//专业
				major = major.trim();
			String subject = (String) row.get(3);//科目
				subject = subject.trim();
			String relatedQuestion = (String)row.get(4);//关联题目信息
			String keywordOne = (String) row.get(5);//关键字1
			String keywordTwo = (String) row.get(6);//关键字2
			String keywordThree = (String) row.get(7);//关键字3
			String answer = (String) row.get(8);//答案详解
			
			questionNew.setId(id);
			if(quesNo == null) {
				response.setCode(QuestionResponse.DATA_NULL_ERROR);
				response.setMessage("第"+(i+2)+"行序号数据缺失，请确认。");
				return response;
			}
			questionNew.setQuesNo(quesNo);
			questionNew.setQuestion(question);
			if(StringUtils.isEmpty(major)) {
				response.setCode(QuestionResponse.DATA_NULL_ERROR);
				response.setMessage("第"+(i+2)+"行所属专业数据缺失，请确认。");
				return response;
			}
			if(StringUtils.isEmpty(majorMaps.get(major))) {
				response.setCode(QuestionResponse.DATA_FORMAT_ERROR);
				response.setMessage("第"+(i+2)+"行所属专业数据非法，请确认。");
				return response;
			}
			questionNew.setMajor(majorMaps.get(major));
			
			if(StringUtils.isEmpty(subject)) {
				response.setCode(QuestionResponse.DATA_NULL_ERROR);
				response.setMessage("第"+(i+2)+"行所属科目数据缺失，请确认。");
				return response;
			}
			if(StringUtils.isEmpty(subjectMaps.get(subject))) {
				response.setCode(QuestionResponse.DATA_FORMAT_ERROR);
				response.setMessage("第"+(i+2)+"行所属科目数据非法，请确认。");
				return response;
			}
			questionNew.setSubject(subjectMaps.get(subject));
			questionNew.setRelatedQuestion(relatedQuestion);
			questionNew.setKeywordOne(keywordOne);
			questionNew.setKeywordTwo(keywordTwo);
			questionNew.setKeywordThree(keywordThree);
			questionNew.setAnswer(answer);
			questionNew.setCreateTime(new Date());
			questions.add(questionNew);
		}
		QuestionResponse questionResponse = ValidateUtil.checkExercises(questions);
		if(!questionResponse.getCode().equals(QuestionResponse.SUCCESS)) {
			return questionResponse;
		}
		try {
			int i = questionNewMapper.batchInsert(questions);
			
			response.setCode(QuestionResponse.SUCCESS);
			response.setMessage("成功插入"+i+"条题目数据");
		} catch (Exception e) {
			response.setCode(QuestionResponse.SERVER_ERROR);
			response.setMessage(e.getCause().getMessage());
			response.setErrorMessage(e.getMessage());
			throw new Exception(e);
		}
		return response;
	}
	
	private Map<String, String> getMajorMaps(){
		List<Dictionary> list = dictionaryMapper.selectByModel("clean_major_type");
		Map<String, String> map = new HashMap<>();
		for (Dictionary dictionary : list) {
			map.put(dictionary.getValue(), dictionary.getCode());
		}
		return map;
	}
	
	private Map<String, String> getSubjectMaps(){
		List<Dictionary> list = dictionaryMapper.selectByModel("clean_subject_type");
		Map<String, String> map = new HashMap<>();
		for (Dictionary dictionary : list) {
			map.put(dictionary.getValue(), dictionary.getCode());
		}
		return map;
	}
	
}
